package com.kxmall.web.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import com.kxmall.common.constant.Constants;
import com.kxmall.common.core.controller.BaseAppController;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.domain.entity.SysMenu;
import com.kxmall.common.core.domain.entity.SysUser;
import com.kxmall.common.core.domain.model.LoginBody;
import com.kxmall.common.core.domain.model.LoginUser;
import com.kxmall.common.core.domain.model.SmsLoginBody;
import com.kxmall.common.helper.LoginHelper;
import com.kxmall.system.domain.vo.RouterVo;
import com.kxmall.web.controller.system.service.impl.SysWebLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 维护人员登录验证
 */
@Api(tags = "维护人员登录验证")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web")
public class SysWebLoginController extends BaseAppController {

    private final SysWebLoginService webLoginService;

    /**
     * 维护人员登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/login")
    @ApiOperation(value = "/web/login - 维护人员登录方法", httpMethod = "POST")
    public R<Map<String, Object>> login(@Validated @RequestBody LoginBody loginBody) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = webLoginService.login(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

    /**
     * 短信登录(示例)
     *
     * @param smsLoginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/smsLogin")
    @ApiOperation(value = "/web/smsLogin - 维护人员短信登录", httpMethod = "POST")
    public R<Map<String, Object>> smsLogin(@Validated @RequestBody SmsLoginBody smsLoginBody) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = webLoginService.smsLogin(smsLoginBody.getPhonenumber(), smsLoginBody.getSmsCode());
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

    /**
     * 退出登录
     */
    @SaIgnore
    @PostMapping("/logout")
    @ApiOperation(value = "/web/logout - 退出登录", httpMethod = "POST")
    public R<Void> logout() {
        webLoginService.logout();
        return R.ok("退出成功");
    }

}
